﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Threading;


public class AniEffectSkillBatter : AniEffectSkillA {
	
	public int batterCount = 2;
	
	public float intervalSecond = 0.2f;

	public override bool playBullet(AniEffectSkill aniEffectSkill, EffectStepObject effectStepObject, bool overrideFlag) {
		if(!overrideFlag) {
			StartCoroutine(playBulletEnumerator(aniEffectSkill, effectStepObject));
		}

		return true;
	}
	
	public IEnumerator playBulletEnumerator(AniEffectSkill aniEffectSkill, EffectStepObject effectStepObject) {
		if(effectStepObject.beginObject != null) {
			Destroy(effectStepObject.beginObject);
			effectStepObject.beginObject = null;
		}
		
		for(int i = 1; i < batterCount; i++) {
			EffectStepObject batterStepObject = new EffectStepObject();
			batterStepObject.index = effectStepObject.index;
			batterStepObject.target = effectStepObject.target;
			batterStepObject.effectStep = effectStepObject.effectStep;
			batterStepObject.stepComplete = getBatterComplete(aniEffectSkill, batterStepObject);
			aniEffectSkill.playBullet(batterStepObject);
			yield return new WaitForSeconds(intervalSecond);
		}
		
		aniEffectSkill.playBullet(effectStepObject);
	}
	
	public static CallBack getBatterComplete(AniEffectSkill aniEffectSkill, EffectStepObject effectStepObject) {
		return ()=>{
			if(effectStepObject.effectStep == EffectStep.COMPLETE){ 
				if(effectStepObject.endObject != null) {
					Destroy(effectStepObject.endObject);
				}
				
			} else {
				aniEffectSkill.playStep(effectStepObject);
			}
		};
	}

}
